package com.ruoyi.hospital.registered.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.hospital.medication.domain.TMedication;
import com.ruoyi.hospital.medication.service.ITMedicationService;
import com.ruoyi.hospital.medicine.domain.TPatientMedicine;
import com.ruoyi.hospital.medicine.service.ITPatientMedicineService;
import com.ruoyi.hospital.order.service.ITOrderService;
import com.ruoyi.hospital.registered.domain.TRegistered;
import com.ruoyi.hospital.registered.mapper.TRegisteredMapper;
import com.ruoyi.hospital.registered.service.ITRegisteredService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 门诊挂号Service业务层处理
 *
 * @author Shawn
 * @date 2024-02-21
 */
@Service
public class TRegisteredServiceImpl implements ITRegisteredService {
    @Autowired
    private TRegisteredMapper tRegisteredMapper;

    @Autowired
    private ITOrderService itOrderService;

    @Autowired
    private ITPatientMedicineService itPatientMedicineService;

    @Autowired
    private ITMedicationService itMedicationService;

    /**
     * 查询门诊挂号
     *
     * @param id 门诊挂号主键
     * @return 门诊挂号
     */
    @Override
    public TRegistered selectTRegisteredById(Long id) {
        return tRegisteredMapper.selectTRegisteredById(id);
    }

    /**
     * 查询门诊挂号列表
     *
     * @param tRegistered 门诊挂号
     * @return 门诊挂号
     */
    @Override
    public List<TRegistered> selectTRegisteredList(TRegistered tRegistered) {
        return tRegisteredMapper.selectTRegisteredList(tRegistered);
    }

    /**
     * 新增门诊挂号
     *
     * @param tRegistered 门诊挂号
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTRegistered(TRegistered tRegistered) {
        List<TRegistered> registeredList = tRegisteredMapper.selectTRegisteredByPatientId(String.valueOf(tRegistered.getPatientId()));
        if (!registeredList.isEmpty()) {
            throw new ServiceException("患者已经挂号");
        }
        tRegistered.setFinish(0l);
        tRegistered.setPrescribe(0L);
        tRegistered.setCreatedTime(new Date());
        tRegistered.setCreatedBy(String.valueOf(ShiroUtils.getUserId()));
        return tRegisteredMapper.insertTRegistered(tRegistered);
    }


    /**
     * 修改门诊挂号
     *
     * @param tRegistered 门诊挂号
     * @return 结果
     */
    @Override
    public int updateTRegistered(TRegistered tRegistered) {
        return tRegisteredMapper.updateTRegistered(tRegistered);
    }

    /**
     * 批量删除门诊挂号
     *
     * @param ids 需要删除的门诊挂号主键
     * @return 结果
     */
    @Override
    public int deleteTRegisteredByIds(String ids) {
        return tRegisteredMapper.deleteTRegisteredByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除门诊挂号信息
     *
     * @param id 门诊挂号主键
     * @return 结果
     */
    @Override
    public int deleteTRegisteredById(Long id) {
        return tRegisteredMapper.deleteTRegisteredById(id);
    }

    @Override
    public int updateIllness(TRegistered tRegistered) {
        TRegistered registered = tRegisteredMapper.selectTRegisteredById(tRegistered.getId());
        //添加门诊订单
        itOrderService.insertRegisteredOrder(registered);
        tRegistered.setFinish(1L);
        return tRegisteredMapper.updateTRegistered(tRegistered);
    }

    @Override
    public int updatePrescribe(TRegistered tRegistered) {
        TPatientMedicine tPatientMedicine = new TPatientMedicine();
        tPatientMedicine.setRegisteredId(String.valueOf(tRegistered.getId()));
        tPatientMedicine.setPatientId(String.valueOf(tRegistered.getPatientId()));
        List<TPatientMedicine> tPatientMedicineList = itPatientMedicineService.selectTPatientMedicineList(tPatientMedicine);
        if (tPatientMedicineList.isEmpty()) {
            throw new ServiceException("还未开药");
        }
        BigDecimal total = new BigDecimal(0);
        for (TPatientMedicine patientMedicine : tPatientMedicineList) {
            TMedication tMedication = itMedicationService.selectTMedicationById(Long.parseLong(patientMedicine.getMedicationId()));
            BigDecimal tmpValue = tMedication.getPrice().multiply(BigDecimal.valueOf(patientMedicine.getQuantity()));
            total = total.add(tmpValue);
        }

        tRegistered.setPrescribe(1L);
        itOrderService.insertMedicationOrder(total, tRegistered);
        return tRegisteredMapper.updateTRegistered(tRegistered);
    }

    @Override
    public Map<String,Object> countRegistered(TRegistered registered) {
        return tRegisteredMapper.countRegistered(registered);
    }
}
